/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.action;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import simplemetrosystem.control.ElementManager;
import simplemetrosystem.model.charlanguage.NameMap;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.view.MetroUpdate;

/**
 *
 * @author Eric Qian
 */
public class SaveAsActionListener implements ActionListener {

    public SaveAsActionListener(MetroFrame frame) {
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("Action:SaveAs");
        nameMap = NameMap.getInstance();
        saveAs();
    }

    public int saveAs() {
        int choice = 0;
        elementManager = ElementManager.getInstance();
        Document doc = elementManager.buildDocument();
        try {
            JFileChooser chooser = new JFileChooser();
            MetroUpdate.updateFont(chooser, new Font(null, Font.PLAIN, 12));
            if (elementManager.getPath() != null) {
                chooser.setSelectedFile(new File(elementManager.getPath()));
            }
            chooser.setFileFilter(new FileFilter() {

                @Override
                public boolean accept(File f) {
                    return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml");
                }

                @Override
                public String getDescription() {
                    return "XML files";
                }
            });
            int returnVal = chooser.showSaveDialog(frame);
            File mapXML = null;
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                mapXML = chooser.getSelectedFile();
            } else {
                return JFileChooser.CANCEL_OPTION;
            }
            if (mapXML.getName().lastIndexOf(".") < 0) {
                mapXML = new File(mapXML.getPath() + ".xml");
            }
            if (mapXML != null && mapXML.exists()) {
                int confirm = JOptionPane.showConfirmDialog(frame,
                        nameMap.getName("File already exists, OVERWRITE?"),
                        nameMap.getName("File Overwrite"),
                        JOptionPane.YES_NO_OPTION);
                switch (confirm) {
                    case JOptionPane.YES_OPTION:
                        saveFile(mapXML, doc);
                        choice = JOptionPane.YES_OPTION;
                        break;
                    case JOptionPane.NO_OPTION:
                        choice = JOptionPane.NO_OPTION;
                        break;
                }
            } else {
                saveFile(mapXML, doc);
            }
            elementManager.setPath(mapXML.getPath());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(frame, nameMap.getName("Error saving the current file"),
                    nameMap.getName("Error"), JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
        return choice;
    }

    public void saveFile(File mapXML, Document doc) {
        //String systemIdentifier = "system";
        //String publicIdentifier = "public";
        try {
            Transformer tran = TransformerFactory.newInstance().newTransformer();
            //tran.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemIdentifier);
            //tran.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, publicIdentifier);
            tran.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(mapXML)));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(frame, "Error saving the current file",
                    "Error", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }
    private MetroFrame frame;
    private NameMap nameMap;
    private ElementManager elementManager;
}
